import { Component, OnInit, ViewChild } from '@angular/core';
import { ApiService } from '../../../core/api.service';
import { NzMessageService, NzModalService } from 'ng-zorro-antd';
import { DomSanitizer } from '@angular/platform-browser';
import { _HttpClient } from '@delon/theme';
import { TableRowMergeComponent } from '../../../core/tableRowMerge.component';
import { MaintenanceComponent } from '../../../layout/components/maintenance.component';
import { environment } from '../../../../environments/environment';

@Component({
  selector: 'app-yearworkarea',
  templateUrl: './yearworkarea.component.html',
  styleUrls: ['./yearworkarea.component.less']
})
export class YearworkareaComponent implements OnInit {
  @ViewChild('mtecom', { static: true }) mtecom: MaintenanceComponent;

  allChecked = false;
  indeterminate = false;
  displayData = [];
  data: any = [];
  yearpageValue = '50';
  yearselectedValue = '50';
  step1: any = true;
  rowTotalTable1 = 0;
  pageIndexTable1 = 1;
  rowpageTable1 = 1;
  pageTotal1 = '';

  displayData2 = [];
  data2: any = [];
  listpageValue = '50';
  listselectedValue = '50';
  step2: any = false;
  rowTotalTable2 = 0;
  pageIndexTable2 = 1;
  rowpageTable2 = 1;
  pageTotal2 = '';

  addYearVisible = false;
  operateYearVisible = false;
  operateyearData = { 'type': '', 'title': '', 'data': [] };
  statusYearVisible = false;
  statusYearNid = '';
  addWorkVisible = false;
  addWorkButton = false;

  getyearlist: any;
  devinfolist: any;
  tempEquipmentValue: any = [];
  tempEquipmentIds: any = [];
  tempEquipmentGroup: any = [];
  tempstepyear = '';
  tempstepstatus = '';
  showstep3 = 'none';
  linetype = '';
  filter = '';
  lockHeadScroll = { y: window.innerHeight - 350 + 'px' };

  constructor(
    private apiService: ApiService,
    private message: NzMessageService,
    private tabmercom: TableRowMergeComponent,
    private http: _HttpClient,
    private sanitizer: DomSanitizer
  ) { }

  ngOnInit() {
    this.reload();
  }

  reload() {
    this.readyload(this.rowpageTable1, this.yearpageValue);
  }

  resetWorkType = (ev: string) => this.filter = ev;

  // 第一页列表数据
  readyload(page, rows) {
    this.pageIndexTable1 = page;
    this.data = [];
    this.yearworkData(page, rows).subscribe((res: any) => {
      if (res.type === 'success') {
        this.data = res.data.pageList;
        if (this.data.length) {
          this.pageTotal1 = res.data.pageCount;
          this.rowTotalTable1 = res.data.pageCount;
          for (const item of this.data) {
            if (item.VC_STATUS == 0 || item.VC_STATUS == 3) {
              item.disabled = false;
            } else {
              item.disabled = true;
            }
          }
        }
      }
    });
  }

  // 年表分页get
  yearworkData(page, rows) {
    const url = environment.apiBase + '/csmis/ymyear/ymtyearwork/getyearmwork.jwt?page=' + page + '&rows=' + rows;
    return this.http.get(url);
  }
  // 第二页列表数据
  startstep(page, year, status, row, filter, linetype, N_ID) {
    this.statusYearNid = N_ID;
    this.linetype = linetype;
    this.rowpageTable2 = page;
    this.pageIndexTable2 = page;
    this.tempstepyear = year;
    this.tempstepstatus = status;
    // 控制显示新增作业按钮
    if (status === 'edit') {
      this.addWorkButton = true;
    }
    if (filter) {
      this.filter = filter;
    }
    this.data2 = [];
    this.yeardetalData(page, year, status, row, this.filter, linetype).subscribe((res: any) => {
      if (res.type === 'success') {
        this.data2 = res.data.pageList;
        if (this.data2.length) {
          this.pageTotal2 = res.data.pageCount;
          this.rowTotalTable2 = res.data.pageCount;
          for (const item of this.data2) {
            // 处理换行
            item.REVC_NOTE = item.VC_NOTE && item.VC_NOTE !== 'null' ? this.sanitizer.bypassSecurityTrustHtml(item.VC_NOTE.replace(/\[br\]/g, '<br>').replace(/\[nbsp\]/g, ' ')) : ' ';
          }
          // 表格合并
          this.tabmercom.mergeRowSet('VC_CLASS,VC_ITEM$NAME,VC_RECYCLE$DESC,VC_UNIT,DEVSUMS,REVC_NOTE', this.data2, (tableData) => {
            this.data2 = tableData;
          });
        }
        this.step1 = false;
        this.step2 = true;
        this.showstep3 = 'none';
      }
    });
  }

  // 年表详情get
  yeardetalData(page, year, status, row, filter, linetype) {
    let url = environment.apiBase + '/csmis/ymyear/ymtyearwork/getyearworkplan.jwt?page=' + page + '&rows=' + row + '&linetype=' + linetype + '&yearnum=' + year + filter;
    if (status == 'edit') {
      url += '&classid=127828';
    }
    return this.http.get(url);
  }
  // 显示[新编]弹出层
  yearShowModal() {
    this.addYearVisible = true;
  }
  yearHideModal() {
    this.addYearVisible = false;
  }
  // 显示[删除]弹出层
  operateShowModal(type, title) {
    this.operateyearData.type = type;
    this.operateyearData.title = title;
    this.operateyearData.data = this.displayData;
    this.operateYearVisible = true;
  }
  operateHideModal() {
    this.operateYearVisible = false;
  }
  deleteItem(id) {
    if (!id) {
      return;
    }
    const param = { nnDID: id };
    this.apiService.yearlyDataDelete(param).subscribe(res => {
      if (res.type === 'success') {
        this.message.success('删除成功');
        this.mainBackPage();
      } else {
        this.message.success('删除失败');
      }
    });
  }

  // 筛选数据
  yearFilterData(data) {
    let url = '';
    if (data.protype == '') {
      this.filter = '';
    }
    data.yearstatus ? url += '&yearstatus=' + data.yearstatus : url += '';
    data.protype ? url += '&protype=' + data.protype : url += '';
    data.level ? url += '&level=' + data.level : url += '';
    this.startstep(this.rowpageTable2, this.tempstepyear, this.tempstepstatus, this.listpageValue, url, this.linetype, this.statusYearNid);
  }

  // 返回第一页
  backstep() {
    this.step1 = true;
    this.step2 = false;
    this.showstep3 = 'none';
    this.pageIndexTable2 = 1;
    this.rowpageTable2 = 1;
    this.addWorkButton = false;
    this.reload();
  }

  // 返回第二页
  backstep2() {
    this.step1 = false;
    this.step2 = true;
    this.showstep3 = 'none';
    this.startstep(this.rowpageTable2, this.tempstepyear, this.tempstepstatus, this.listpageValue, '', this.linetype, this.statusYearNid);
  }

  mainBackPage() {
    this.startstep(this.rowpageTable2, this.tempstepyear, this.tempstepstatus, this.listpageValue, '', this.linetype, this.statusYearNid);
  }

  // 分页数量-第一页
  yearselectedChange() {
    this.yearpageValue = this.yearselectedValue;
    this.reload();
  }

  // 分页数量-第二页
  listselectedChange() {
    this.listpageValue = this.listselectedValue;
    this.startstep(this.rowpageTable2, this.tempstepyear, this.tempstepstatus, this.listpageValue, '', this.linetype, this.statusYearNid);
  }

  // 第三页详情数据
  maintain(index, itemid, nid, type, orgId) {
    this.mtecom.getData(this.data2, index, itemid, nid, type, (yearlist, devlist) => {
      this.getyearlist = yearlist;
      this.devinfolist = devlist;
    }, orgId);
    this.step1 = false;
    this.step2 = false;
    this.showstep3 = 'block';
  }

  currentPageDataChange($event: Array<{ name: string; age: number; address: string; checked: boolean; disabled: boolean; }>): void {
    this.displayData = $event;
    this.refreshStatus();
  }

  refreshStatus(): void {
    const allChecked = this.displayData.filter(value => !value.disabled).every(value => value.checked == true);
    const allUnChecked = this.displayData.filter(value => !value.disabled).every(value => !value.checked);
    this.allChecked = allChecked;
    this.indeterminate = (!allChecked) && (!allUnChecked);
  }

  rowChangeTable1(ev) {
    this.rowpageTable1 = ev;
    this.reload();
  }

  rowChangeTable2(ev) {
    this.rowpageTable2 = ev;
    this.startstep(this.rowpageTable2, this.tempstepyear, this.tempstepstatus, this.listpageValue, '', this.linetype, this.statusYearNid);
  }

  // 全选
  checkAll(value: boolean): void {
    this.displayData.forEach(data => {
      if (!data.disabled) {
        data.checked = value;
      }
    });
    this.refreshStatus();
  }
  // 显示[状态]弹出层
  statusShowModal(nid) {
    this.statusYearNid = nid;
    this.statusYearVisible = true;
  }
  statusHideModal() {
    this.statusYearVisible = false;
  }
  // 显示[作业项目]弹出层
  addWorkShowModal() {
    this.addWorkVisible = true;
  }
  addWorkHideModal() {
    this.addWorkVisible = false;
  }
}
